package com.xsy.de.restful.controller;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xsy.de.util.HttpClientUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author DingHao
 * @since 2019-07-07
 */
@RestController
@RequestMapping("/chargeUser")
@Api(tags = "充电用户操作接口")
public class ChargeUserController {
    private final static Logger log = LogManager.getLogger(ChargeUserController.class);


    /**
     * 获取一个用户
     * @return
     */
    @ApiOperation(value="获取一个用户", notes="")
    @RequestMapping(value = "/useUser", method = RequestMethod.POST)
    public JSONObject useUser(){
        try {
            //        String uri = "https://firm.echargenet.com/corp/json/cus_corp/getCusUserList.action?randomTimeTemp=" + System.currentTimeMillis();
            String uri = "https://firm.echargenet.com/corp/json/cus_corp/getAcctList.action";
            Map<String, String> map = new HashMap<>();
            map.put("randomTimeTemp",System.currentTimeMillis()+"");
            log.info("请求后台用户列表获取接口:"+uri);
            String result = HttpClientUtil.doPost(uri,map);
            log.info("请求后台用户列表获取接口-返回结果:"+result);
            JSONObject jsonResult = JSONObject.parseObject(result);
            JSONArray userList = jsonResult.getJSONArray("rows");
            for (int i = 0; i < userList.size(); i++) {
                JSONObject user = userList.getJSONObject(i);
                if ("1".equals(user.getString("accountAcctStatus"))){
                    JSONObject returnResult = new JSONObject();
                    returnResult.put("userId",user.getString("userId"));
                    returnResult.put("userName",user.getString("userName"));
                    return returnResult;
                }
            }
        }catch (Exception e){
            return null;
        }
        return null;
    }

    /**
     * 锁定用户
     * @param userId
     * @return
     */
    @ApiOperation(value="锁定一个用户", notes="")
    @ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "String", paramType = "query")
    @RequestMapping(value = "/lockUser", method = RequestMethod.POST)
    public boolean lockUser(@RequestParam String userId){
        try {
            String uri = "https://firm.echargenet.com/corp/json/cus_corp/freezeAcctBase.action";
            Map<String, String> map = new HashMap<>();
            map.put("userId",userId);
            map.put("randomTimeTemp",System.currentTimeMillis()+"");
            log.info("请求后台锁定用户接口:"+uri);
            String result = HttpClientUtil.doPost(uri,map);
            log.info("请求后台锁定用户接口-返回结果:"+result);
            JSONObject jsonResult = JSONObject.parseObject(result);
            if (jsonResult.getInteger("status") == 0){
                return true;
            }
        }catch (Exception e){
            return false;
        }
        return false;
    }

    /**
     * 解锁用户
     * @param userId
     * @return
     */
    @ApiOperation(value="解锁一个用户", notes="")
    @ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "String", paramType = "query")
    @RequestMapping(value = "/unLockUser", method = RequestMethod.POST)
    public boolean unLockUser(@RequestParam String userId){
        try {
            String uri = "https://firm.echargenet.com/corp/json/cus_corp/unfreezeAcctBase.action";
            Map<String, String> map = new HashMap<>();
            map.put("userId",userId);
            map.put("randomTimeTemp",System.currentTimeMillis()+"");
            log.info("请求后台解锁用户接口:"+uri);
            String result = HttpClientUtil.doPost(uri,map);
            log.info("请求后台解锁用户接口-返回结果:"+result);
            JSONObject jsonResult = JSONObject.parseObject(result);
            if (jsonResult.getInteger("status") == 0){
                return true;
            }
        }catch (Exception e){
            return false;
        }

        return false;
    }

}

